#!/bin/bash

set -x

# default to update source then build

TOP_DIR=$(pwd)
SRC_DIR=$TOP_DIR/lib_source
BD_DIR=$TOP_DIR/lib_build
BIN_DIR=$TOP_DIR/lib_bin

VER_OPT=1.2

export PATH=$TOP_DIR/cross/bin:$PATH

cmd=all64
if [ -n "$1" ] ; then
	cmd=$1
fi

if [ "$cmd" == "all64" ] || [ "$cmd" == "all32" ] || [ "$cmd" == "update" ] ; then
	mkdir -p $SRC_DIR || exit 0
	cd $SRC_DIR
	if [ ! -f pugixml-$VER_OPT.tar.gz ] ; then
		wget http://pugixml.googlecode.com/files/pugixml-$VER_OPT.tar.gz
	fi
	cd ..
fi

if [ "$cmd" == "all64" ] || [ "$cmd" == "b64" ] || [ "$cmd" == "all32" ] || [ "$cmd" == "b32" ] ; then
	CROSS="x86_64-w64-mingw32-"
	if [ "$cmd" == "all32" ] || [ "$cmd" == "b32" ] ; then
		CROSS="i686-w64-mingw32-"
	fi
	rm -rf $BIN_DIR/pugixml
	mkdir -p $BD_DIR
	cd $BD_DIR
	rm -rf bd_pugixml
	mkdir bd_pugixml
	cd bd_pugixml
	tar xzf $SRC_DIR/pugixml-$VER_OPT.tar.gz
	patch -p0 < $TOP_DIR/patch/pugixml.patch
	${CROSS}g++ -shared -o pugixml.dll -Wl,--out-implib,libpugixml.dll.a src/*.cpp
	mkdir -p $BIN_DIR/pugixml/include
	cp src/*.hpp $BIN_DIR/pugixml/include
	mkdir -p $BIN_DIR/pugixml/bin
	cp pugixml.dll $BIN_DIR/pugixml/bin
	mkdir -p $BIN_DIR/pugixml/lib
	cp libpugixml.dll.a $BIN_DIR/pugixml/lib
	cd ..

	cd ..
fi

